﻿using System;
using System.IO;
using System.Threading;
using DatiPluginLib;
using Mrsoft.Common;

namespace Mrsoft.Core {
    /// <summary>
    /// 慧眼答题
    /// </summary>
    public sealed class HuiYan : Dati, IDati {

        /// <summary>
        /// 答题
        /// </summary>
        /// <param name="datiModel">题目原始信息</param>
        /// <returns>题目处理后的答案信息 如果失败了 就返回失败原因</returns>
        public DatiResult Answer(DatiModel datiModel) {
            //如果发送成功了返回的就是questionid
            var result = SendPic(datiModel);
            if (!result.OK) {
                return result;
            }
            //等待答案
            var start = DateTime.Now;
            while (true) {

                string answer = hy.GetAnswer(result.Message);
                if (!string.IsNullOrEmpty(answer)) {
                    if (IsRight(answer)) {
                        return new DatiResult(true, answer);
                    }
                    break;
                }

                if ((DateTime.Now - start).TotalSeconds > datiModel.Timeout)
                    break;
                Thread.Sleep(200);
            }
            return new DatiResult(false, "答题超时");
        }

        /// <summary>
        /// 发送答题请求
        /// </summary>
        /// <param name="datiModel">答题数据信息</param>
        /// <returns>返回请求结果 如果成功返回题目串号 否则返回失败原因</returns>
        private DatiResult SendPic(DatiModel datiModel) {

            NativeDatiModel(datiModel);

            if (hy == null) {
                return new DatiResult(false, "答题插件不可以用");
            }

            DatiResult result = AskDati();
            if (!result.OK)
                return result;

            string rs = "未知错误";
            for (int i = 0; i < 10; i++) {
                rs = hy.SendFile(_authorKey, datiModel.ImageFullPath, datiModel.QuestinType, datiModel.Timeout, 1, datiModel.TipsMessage);
                if (IsRight(rs)) {
                    return new DatiResult(true, rs);
                }
                Thread.Sleep(500);
            }

            LosePoint("error");//返还题分
            return new DatiResult(false, rs);
        }

        /// <summary>
        /// 将本地统一的题目类型转换到网站特定的类型
        /// </summary>
        /// <param name="datiModel"></param>
        private void NativeDatiModel(DatiModel datiModel) {

        }

        /// <summary>
        /// 慧眼答题初始化
        /// </summary>
        /// <param name="authorKey"></param>
        public HuiYan() {
            if (InitCom()) {

                var author1 = "ZN6wqzakUfLlL22V";//mrsoft
                var author2 = "jKCpWNhFc8LaSVNF";//a10909
                _authorKey = ("a10909" == user) ? author1 : author2;
                if ("a10909" == user)
                    hy.SetAuthor("mrsoft");
                else
                    hy.SetAuthor("a10909");

                ShowMessage("插件加载成功");
                //try {
                //    //var money1 = int.Parse(hy.QueryBalance(author1));
                //    //var money2 = int.Parse(hy.QueryBalance(author2));
                //    //_authorKey = (money1 > money2) ? author1 : author2;
                //    //ShowMessage("用户账户余额:" + money1);
                //}
                //catch {
                //    _authorKey = author1;
                //}


            }
        }

        #region 私有变量

        /// <summary>
        /// 用户KEY
        /// </summary>
        private readonly string _authorKey;

        /// <summary>
        /// 慧眼答题对象
        /// </summary>
        private DatiPlugin hy;
        #endregion

        /// <summary>
        /// 封装了慧眼COM的注册函数
        /// </summary>
        /// <returns></returns>
        protected override bool InitCom() {
            try {
                try {
                    hy = new DatiPlugin();
                }
                catch {
                    NativeMethods.ComRegister(Directory.GetCurrentDirectory() + @"\插件\hydati.dll");
                    hy = new DatiPlugin();
                }
                MsgListener.CommonMsgShow("答题系统", "准备完成");
                return true;
            }
            catch (Exception ex) {
                MsgListener.MsgBoxMsgShow(ex.Message, "答题插件错误");
                return false;
            }
        }

        /// <summary>
        /// 判断返回字符串是否为正确的
        /// </summary>
        /// <param name="temAnswer"></param>
        /// <returns></returns>
        private bool IsRight(string temAnswer) {
            return !string.IsNullOrEmpty(temAnswer) && !temAnswer.StartsWith("#");
        }

        /// <summary>
        /// 查询答题余额
        /// </summary>
        public int Points {
            get { return CheckPoint(); }
        }

        /// <summary>
        /// 用于判断慧眼的COM是否可用
        /// </summary>
        public bool IsReady {
            get { return (hy != null); }
        }
    }
}